//
//  SCanMaskView.swift
//  hkrt
//
//  Created by 文 on 2024/2/5.
//

import Foundation
import UIKit

let screenWidth: CGFloat = UIScreen.main.bounds.size.width
let screenHeight: CGFloat = UIScreen.main.bounds.size.height
let screenRadio: CGFloat  = screenWidth / 375.0
let scanQRCodeWidth: CGFloat = 280 * screenRadio
let scanQRCodeTop: CGFloat = 124 * screenRadio

class SCanMaskView: UIView {
    
    lazy var scanLineView : UIView = {
        let vi = UIView()
        vi.backgroundColor = .THEME
        
        return vi
        
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .clear
        
//        addSubview(scanLineView)
//        scanLineView.snp.makeConstraints { make in
//            make.centerX.lessThanOrEqualToSuperview()
//            make.top.equalTo(scanQRCodeTop+20*screenRadio)
//            make.width.equalTo(313*screenRadio)
//            make.height.equalTo(6*screenRadio)
//        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func startLineAnimation() {
        scanLineView.layer.removeAllAnimations()
        let animation = CABasicAnimation.init(keyPath: "transform")
        animation.toValue = NSValue.init(caTransform3D:CATransform3DMakeTranslation(0, scanQRCodeWidth-40*screenRadio, 1))
        animation.duration = 4
        animation.repeatCount = MAXFLOAT
        animation.autoreverses = true
//        scanLineView.layer.addAnimations(animation)
        
    }
    
    override func draw(_ rect: CGRect) {
        guard let con = UIGraphicsGetCurrentContext() else {
            return
        }
        con.setFillColor(UIColor.black.withAlphaComponent(0.5).cgColor)
        con.fill(rect)
        con.clear(CGRect(x: (screenWidth - scanQRCodeWidth) / 2.0, y: scanQRCodeTop, width: scanQRCodeWidth, height: scanQRCodeWidth))
        
        //设置线条样式
        con.setLineCap(.square)
        //设置线条粗细宽度
        con.setLineWidth(2.0)
        
        //设置颜色
//        con.setStrokeColor(UIColor(red: 229 / 255.0, green: 0, blue: 50 / 255.0, alpha: 1.0).cgColor)
        con.setStrokeColor(UIColor.white.cgColor)
        //开始一个起始路径
        con.beginPath()
        //起始点设置为(100,100):注意这是上下文对应区域中的相对坐标，
        con.move(to: CGPoint(x: (screenWidth - scanQRCodeWidth) / 2 + 20, y: scanQRCodeTop))
        //设置下一个坐标点
        con.addLine(to: CGPoint(x:  (screenWidth - scanQRCodeWidth ) / 2, y: scanQRCodeTop))
        //设置下一个坐标点
        con.addLine(to: CGPoint(x: (screenWidth - scanQRCodeWidth ) / 2, y: scanQRCodeTop + 20))
        //连接上面定义的坐标点
        con.strokePath()
        
        //开始一个起始路径
        con.beginPath()
        //起始点设置为(100,100):注意这是上下文对应区域中的相对坐标，
        con.move(to: CGPoint(x: (screenWidth - scanQRCodeWidth) / 2 + scanQRCodeWidth - 20, y: scanQRCodeTop))
        //设置下一个坐标点
        con.addLine(to: CGPoint(x:  (screenWidth - scanQRCodeWidth ) / 2 + scanQRCodeWidth , y: scanQRCodeTop))
        //设置下一个坐标点
        con.addLine(to: CGPoint(x: (screenWidth - scanQRCodeWidth ) / 2 + scanQRCodeWidth, y: scanQRCodeTop + 20))
        //连接上面定义的坐标点
        con.strokePath()
        
        //开始一个起始路径
        con.beginPath()
        //起始点设置为(100,100):注意这是上下文对应区域中的相对坐标，
        con.move(to: CGPoint(x: (screenWidth - scanQRCodeWidth) / 2 + scanQRCodeWidth - 20, y: scanQRCodeTop + scanQRCodeWidth))
        //设置下一个坐标点
        con.addLine(to: CGPoint(x:  (screenWidth - scanQRCodeWidth ) / 2 + scanQRCodeWidth, y: scanQRCodeTop + scanQRCodeWidth))
        //设置下一个坐标点
        con.addLine(to: CGPoint(x: (screenWidth - scanQRCodeWidth ) / 2 + scanQRCodeWidth, y: scanQRCodeTop + scanQRCodeWidth - 20))
        //连接上面定义的坐标点
        con.strokePath()
        
        //开始一个起始路径
        con.beginPath()
        //起始点设置为(100,100):注意这是上下文对应区域中的相对坐标，
        con.move(to: CGPoint(x: (screenWidth - scanQRCodeWidth) / 2 + 20, y: scanQRCodeTop + scanQRCodeWidth))
        //设置下一个坐标点
        con.addLine(to: CGPoint(x:  (screenWidth - scanQRCodeWidth ) / 2, y: scanQRCodeTop + scanQRCodeWidth))
        //设置下一个坐标点
        con.addLine(to: CGPoint(x: (screenWidth - scanQRCodeWidth ) / 2, y: scanQRCodeTop + scanQRCodeWidth - 20))
        //连接上面定义的坐标点
        con.strokePath()
        
    }
    
}
